package com.changgou.search.controller;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Page;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private SearchService searchService;

    @GetMapping("/list")
    public String list(@RequestParam Map<String,String> searchMap, Model model){
        this.handleSearchMap(searchMap);
        Map resultMap = searchService.search(searchMap);
        //对specs集合的格式进行调整
        HashMap<String, Set> specMap = new HashMap<>();
        if(resultMap.containsKey("specs")){
            List<String> specs = (ArrayList<String>)resultMap.get("specs");
            for (String spec : specs) {
                Map<String,String> map = JSON.parseObject(spec, Map.class);
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    String key = entry.getKey();
                    Set<String> set = specMap.get(key);
                    if(set==null){
                        set=new HashSet<String>();
                    }
                    set.add(entry.getValue());
                    specMap.put(key,set);
                }
            }
        }
        StringBuilder stringBuilder = new StringBuilder("/search/list?");
        if(searchMap!=null&&searchMap.size()>0){
            for (Map.Entry<String, String> entry : searchMap.entrySet()) {
                if(!"pageNum".equals(entry.getKey())&&!"sortField".equals(entry.getKey())&&!"sortRule".equals(entry.getKey())){
                    stringBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
                }
            }
        }
        String s = stringBuilder.toString();
        String urlString = s.substring(0,s.length()-1);

        Page<SkuInfo> page = new Page<SkuInfo>(Long.parseLong(String.valueOf(resultMap.get("total"))),
                                            Integer.parseInt(String.valueOf(resultMap.get("pageNum"))),
                                            Page.pageSize);

        resultMap.put("url",urlString);
        resultMap.put("specMap",specMap);
        model.addAttribute("searchMap",searchMap);
        model.addAttribute("result",resultMap);
        model.addAttribute("page",page);
        return "search";
    }

    /**
     * 搜索业务
     * @param searchMap
     * @return
     */
    @GetMapping
    @ResponseBody
    public Map search(@RequestParam Map<String,String> searchMap){
        this.handleSearchMap(searchMap);
        Map searchResult = searchService.search(searchMap);
        return searchResult;
    }

    private void handleSearchMap(Map<String, String> searchMap) {
        Set<Map.Entry<String, String>> entries = searchMap.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            if (entry.getKey().startsWith("spec_")){
                searchMap.put(entry.getKey(),entry.getValue().replace("+","%2B"));
            }
        }
    }

}
